package com.example.myapplication.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.adp.CustomBannerHolder;
import com.example.myapplication.adp.GetCommentAdp;
import com.example.myapplication.database.GetComment;
import com.example.myapplication.database.GetItem;
import com.example.myapplication.database.User;
import com.example.myapplication.util.DbUtils;
import com.example.myapplication.util.GlideUtil;
import com.example.myapplication.util.GsonHelper;
import com.example.myapplication.util.LogUtils;
import com.example.myapplication.util.PhotoUtil;
import com.example.myapplication.util.TimeUtil;
import com.example.myapplication.util.ToastUtil;
import com.example.myapplication.window.HintDialog;
import com.ms.banner.Banner;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class GetDetailAty extends BaseActivity {
    private View headView,btOut;
    private ImageView userIcon;
    private Banner banner;
    private TextView userName,tvDes,tvTime,btAsk;
    private EditText etPrice;
    private EditText etDes;

    private RecyclerView listView;
    private GetCommentAdp adp;
    private ArrayList<GetComment> commentData= new ArrayList<>();
    private GetItem getItem;
    @Override
    protected void logicStart() {
        getItem=LitePal.find(GetItem.class,getIntent().getLongExtra("id",0l));
        LogUtils.log("fhp", GsonHelper.toJsonStr(getItem));
        User user = LitePal.find(User.class, getItem.getPubUser());
        GlideUtil.loadHead(user.getAva(),userIcon);
        userName.setText(user.getName());
        tvDes.setText(getItem.getDes());
        tvTime.setText(getItem.getTime());

        if (null==getItem.getPhotos()||getItem.getPhotos().isEmpty()){
            banner.setVisibility(View.GONE);
        }else {
            banner.setVisibility(View.VISIBLE);
            banner.setPages(getItem.getPhotos(),new CustomBannerHolder())
                    .start();
        }

        commentData.clear();
        List<GetComment> comments = LitePal.where("fatherId = '" + getItem.getId() + "'").find(GetComment.class);
        commentData.addAll(comments);

        adp.notifyDataSetChanged();

        if (getItem.getPubUser()==DbUtils.getLoginUser().getId()){
            btAsk.setVisibility(View.GONE);
        }
        if (getItem.isFinish()){
            View view = LayoutInflater.from(this).inflate(R.layout.get_detail_ept_finish, null);
            TextView tp = view.findViewById(R.id.tvPrice);
            TextView tvFinishUser = view.findViewById(R.id.tvFinishUser);
            TextView tvMoblie= view.findViewById(R.id.tvMoblie);
            TextView tvStatus= view.findViewById(R.id.tvStatus);
            TextView btContact= view.findViewById(R.id.btContact);
            TextView btConfirm= view.findViewById(R.id.btConfirm);
            View btContainer= view.findViewById(R.id.btContainer);

            GetComment comment = LitePal.find(GetComment.class, getItem.getFinishedId());
            User commentUser = LitePal.find(User.class, comment.getUserId());
            if (getItem.isConfirm()){
                //已确认
                tvStatus.setText("已完成");
                btContainer.setVisibility(View.GONE);
            }else {
                tvStatus.setText("待收货");
                btContainer.setVisibility(View.VISIBLE);

                tp.setText("￥"+comment.getPrice());
                tvFinishUser.setText(commentUser.getName());
                tvMoblie.setText(commentUser.getPhone());
                btContact.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoUtil.callPhone(GetDetailAty.this,commentUser.getPhone());
                    }
                });
                btConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getItem.setConfirm(true);
                        getItem.update(getItem.getId());
                        //转钱
                        commentUser.setMoney(user.getMoney()+Integer.parseInt(comment.getPrice()));
                        commentUser.update(comment.getId());
                        ToastUtil.toast("确认收货成功");

                        tvStatus.setText("已完成");
                        btContainer.setVisibility(View.GONE);
                    }
                });
            }

            commentData.clear();
            adp.notifyDataSetChanged();
            adp.setEmptyView(view);
        }else {

            adp.setEmptyView(R.layout.list_ept);
        }
    }

    @Override
    protected void initView() {
        headView= LayoutInflater.from(this).inflate(R.layout.get_detail_head,null);
        userIcon=headView.findViewById(R.id.userIcon);
        userName=headView.findViewById(R.id.userName);
        tvDes=headView.findViewById(R.id.tvDes);
        tvTime=headView.findViewById(R.id.tvTime);
        btAsk=findViewById(R.id.btAsk);
        btOut=findViewById(R.id.btOut);
        etPrice = findViewById(R.id.tvPrice);
        etDes =findViewById(R.id.etDes);

        banner=headView.findViewById(R.id.banner);
        listView=findViewById(R.id.listView);


        adp=new GetCommentAdp(commentData);

        listView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        listView.setAdapter(adp);

        adp.setHeaderView(headView);
        adp.setHeaderWithEmptyEnable(true);

    }

    @Override
    protected void setListener() {
        super.setListener();
        adp.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                new HintDialog(GetDetailAty.this).showIt("确认支付报价？获取联系方式？", new HintDialog.CallBack() {
                    @Override
                    public void yes() {
                            //设置已接受
                        //检查余额够不够

                        User loginUser = DbUtils.getLoginUser();
                        if (loginUser.getMoney()<Integer.parseInt(commentData.get(position).getPrice())){
                            ToastUtil.toast("您的余额不足");
                            return;
                        }

                        loginUser.setMoney(loginUser.getMoney()-Integer.parseInt(commentData.get(position).getPrice()));
                        loginUser.update(loginUser.getId());


                        getItem.setFinish(true);
                        getItem.setFinishedId(commentData.get(position).getId());
                        getItem.update(getItem.getId());
                        logicStart();
                    }
                });
            }
        });
        btOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btOut.setVisibility(View.GONE);

            }
        });

        btAsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getItem.getPubUser()==DbUtils.getLoginUser().getId()){
                    ToastUtil.toast( "不能买自己的东西");
                    return;
                }
                if (!btOut.isShown()){
                    btOut.setVisibility(View.VISIBLE);
                }else {

                    GetComment getComment = new GetComment(
                            DbUtils.getLoginUser().getId(),
                            TimeUtil.getTime(),
                            etDes.getText().toString(),
                            etPrice.getText().toString(),
                            getItem.getId()
                    );
                    getComment.save();
                    logicStart();
                    btOut.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void refreshState(Bundle Bundle) {
        super.refreshState(Bundle);
        commentData.clear();
        List<GetComment> comments = LitePal.where("fatherId = '" + getItem.getId() + "'").find(GetComment.class);
        commentData.addAll(comments);

        adp.notifyDataSetChanged();
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_get_detail;

    }
}
